<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zead_Manifest_Writer_Abstract **/
require_once 'Zead/Manifest/Writer/Abstract.php';
 
/**
 * @category   Zead
 * @package    Zead_Manifest
 * @subpackage Zead_Manifest_Adapter
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License 
 */
class Zead_Manifest_Writer_Xml extends Zead_Manifest_Writer_Abstract
{	
	public function write(Zead_Manifest $manifest, array $options = array())
	{
		$simplexml = new SimpleXmlElement('<manifest/>');

		// Parse contents
		$contents = $simplexml->addChild('contents');	
		
		foreach ($manifest->getContents()->getChildren() as $child) {
			$content = $contents->addChild('content');
			$content->addAttribute('name', $child->name);
			$content->addChild('title', $child->title);
		}
		
		// Parse options
		$options = $simplexml->addChild('options');
		
		foreach ($manifest->getOptions()->getChildren() as $child) {
			$option = $options->addChild($child->name);
		}		
		
		if (isset($this->_options['file'])) {

			$doc = new DOMDocument();
			$doc->appendChild($doc->importNode(dom_import_simplexml($simplexml),true));
			$doc->formatOutput = true;
			$doc->save($this->_options['file']);
		}		
	}
}
